package com.chenggong.animation.utils

import android.animation.*
import android.view.View
import android.view.animation.DecelerateInterpolator

/**
 * @Auther admin
 * @Date 2021/4/9/14:39
 * @Description 为当前类写一句说明
 */
object AnimationUtils {
    /**
     * 抖动动画
     */
    fun shake(
        view: View,
        shakeDegrees: Float = 2.0f,
        duration: Long = 800L
    ) {
        if (view == null) {
            return
        }
        //先往左再往右
        val rotateValuesHolder = PropertyValuesHolder.ofKeyframe(
            View.ROTATION,
            Keyframe.ofFloat(0f, 0f),
            Keyframe.ofFloat(0.2f, -shakeDegrees),
            Keyframe.ofFloat(0.4f, shakeDegrees),
            Keyframe.ofFloat(0.6f, -shakeDegrees),
            Keyframe.ofFloat(0.8f, shakeDegrees),
            Keyframe.ofFloat(1.0f, 0f)
        )
        val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(
            view,
            rotateValuesHolder
        )
        objectAnimator.duration = duration
        objectAnimator.start()
    }

    /**
     * 缩放动画
     */
    fun zoom(
        view: View,
        scaleSmall: Float = 0.8f,
        scaleLarge: Float = 1.0f,
        duration: Long = 800L
    ) {
        if (view == null) {
            return
        }
        //先变小后变大
        val scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(
            View.SCALE_X,
            Keyframe.ofFloat(0f, 1.0f),
            Keyframe.ofFloat(0.25f, scaleSmall),
            Keyframe.ofFloat(0.5f, scaleLarge),
            Keyframe.ofFloat(0.75f, scaleSmall),
            Keyframe.ofFloat(1.0f, 1.0f)
        )
        val scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(
            View.SCALE_Y,
            Keyframe.ofFloat(0f, 1.0f),
            Keyframe.ofFloat(0.25f, scaleSmall),
            Keyframe.ofFloat(0.5f, scaleLarge),
            Keyframe.ofFloat(0.75f, scaleSmall),
            Keyframe.ofFloat(1.0f, 1.0f)
        )
        val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(
            view,
            scaleXValuesHolder,
            scaleYValuesHolder
        )
        objectAnimator.duration = duration
        objectAnimator.start()
    }

    /**
     * 心跳动画
     */
    fun heartBeat(view: View):ObjectAnimator {
        val scaleX = PropertyValuesHolder.ofFloat( View.SCALE_X, 1.0f, 0.8f, 1.0f)
        val scaleY = PropertyValuesHolder.ofFloat( View.SCALE_Y, 1.0f, 0.8f, 1.0f)
        val anim = ObjectAnimator.ofPropertyValuesHolder(view, scaleX, scaleY)
        anim.duration = 1000
        anim.repeatCount = ValueAnimator.INFINITE
        anim.interpolator = DecelerateInterpolator()
        anim.startDelay = 100
        anim.start()
        //复原
        anim.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {}
            override fun onAnimationEnd(animation: Animator?) {
                view.scaleX = 1.0f
                view.scaleY = 1.0f
            }
            override fun onAnimationCancel(animation: Animator?) {
                view.scaleX = 1.0f
                view.scaleY = 1.0f
            }
            override fun onAnimationRepeat(animation: Animator?) {}

        })
        return anim
    }


}